package com.property.business.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.property.business.common.Result;
import com.property.business.entity.Vote;
import com.property.business.service.IVoteService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 业务-投票 前端控制器
 * </p>
 *
 * @author 刘煦煌
 * @since 2023-11-24
 */
@RestController
@RequestMapping("/vote")
public class VoteController {

    @Resource
    private IVoteService voteService;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Vote vote) {
        voteService.saveOrUpdate(vote);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        voteService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        voteService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    public Result findAll() {
        List<String> list = new ArrayList<>();
        list.addAll(Arrays.asList("一", "二", "三"));
        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(voteService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Vote> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(voteService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

